home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Frameworks / Hsoi's App Shell 1.0a4 / Hsoi's App Shell Source / HASLongControls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-28  |  5.7 KB  |  258 lines  |  [TEXT/CWIE]

  1. /*    
  2.     HASLongControls.c from Hsoi's App Shell ©1995-1997 John C. Daub.  All rights reserved.
  3.     
  4.     This file is based upon the WASTE Demo App's LongControls.c by Marco Piovanelli.
  5.     The functions here help us to properly deal with some "short-comings" (there's
  6.     a lame pun in there) of the MacOS.
  7.     
  8.     Normally, controls use shorts to hold the values of the controls.  Unfortunately,
  9.     for scroll bars this is a problem.  Since WASTE allows us to have more than
  10.     32k of text, we could conceivably have scroll bar values that would need to be
  11.     larger than the upper limit of a 2-byte signed short (32,767).  These functions
  12.     herein allow us to use 4-byte signed longs to retain the value of the scroll
  13.     bars.  Furthermore, these functions allow us to co-exist happily with our
  14.     longs and the MacOS.
  15.     
  16.     So, give these a look over, and see how they're implimented throughout the
  17.     rest of the app shell.  You'll see how they work.
  18. */
  19.  
  20. #pragma mark ••• #includes •••
  21.  
  22. #ifndef _WASTE_
  23. #include "WASTE.h"
  24. #endif
  25. #include "HASGlobals.h"
  26. #ifndef __HSOIS_APP_SHELL__
  27. #include "HASMain.h"
  28. #endif
  29. #include "HASLongControls.h"
  30. #include "HASUtilities.h"
  31.  
  32.  
  33. #pragma mark -
  34. #pragma mark ••• Creation/Destruction •••
  35.  
  36. /*
  37.  *    This will create our long control
  38.  */
  39.  
  40. OSErr    HsoiLCAttach( ControlRef control )
  41. {
  42.     Handle        aux = nil;
  43.     LCAuxPtr    pAux;
  44.     OSErr        reply = noErr;
  45.     
  46.     /*    allocate the auxiliary record that will hold long settings */
  47.     
  48.     aux = NewHandleClear( sizeof( LCAuxRec ) );
  49.     if ( aux == nil )
  50.     {
  51.         reply = MemError();
  52.         return    reply;
  53.     }
  54.     
  55.     /*    store a handle to the auxiliary record in the contrlRfCon field */
  56.     
  57.     SetControlReference( control, (long)aux);
  58.         
  59.     /*    copy current control settings into the auxiliary record */
  60.     
  61.     pAux = *((LCAuxHandle)aux);
  62.     pAux->value = GetControlValue( control );
  63.     pAux->min = GetControlMinimum( control );
  64.     pAux->max = GetControlMaximum( control );
  65.     
  66.     return reply;
  67. }
  68.  
  69.  
  70. // detroy a long control
  71.  
  72. void    HsoiLCDetach( ControlRef control )
  73. {
  74.     Handle            aux;
  75.     
  76.     aux = (Handle)GetControlReference( control );
  77.         
  78.     if ( aux != nil )
  79.     {
  80.         SetControlReference( control, 0 );
  81.         
  82.         HsoiForgetHandle( &aux );
  83.     }
  84.     
  85.     return;
  86. }
  87.  
  88. #pragma mark -
  89. #pragma mark ••• Set Values •••
  90.  
  91. // set the value of a long control
  92.  
  93. void    HsoiLCSetValue( ControlRef control, long value )
  94. {
  95.     LCAuxPtr        pAux;
  96.     short            controlMin, controlMax, newControlValue;
  97.     
  98.     pAux = *((LCAuxHandle)GetControlReference( control ));
  99.     
  100.     /*    make sure value is in the range min...max */
  101.     
  102.     if ( value < pAux->min )
  103.         value = pAux->min;
  104.     if ( value > pAux->max )
  105.         value = pAux->max;
  106.     
  107.     /*    save value in auxiliary record */
  108.     
  109.     pAux->value = value;
  110.     
  111.     /*    calculate new thumb position */
  112.     
  113.     controlMin = GetControlMinimum( control );
  114.     controlMax = GetControlMaximum( control );
  115.     newControlValue = controlMin + FixRound( FixMul ( FixDiv( value - pAux->min, 
  116.                 pAux->max - pAux->min), BSL(controlMax - controlMin, 16 )));
  117.     
  118.     /*    do nothing if the thumb position hasn't changed */
  119.     
  120.     if ( newControlValue != GetControlValue( control ) )
  121.         SetControlValue( control, newControlValue );
  122.  
  123.     return;
  124. }
  125.  
  126. // set the minimum value of a long control
  127.  
  128. void    HsoiLCSetMin( ControlRef control, long min )
  129. {
  130.     LCAuxPtr        pAux;
  131.     
  132.     pAux = *((LCAuxHandle)GetControlReference( control ));
  133.     
  134.     /*    make sure min is less than or equal to max */
  135.     
  136.     if ( min > pAux->max )
  137.         min = pAux->max;
  138.     
  139.     /*    save min in auxiliary record */
  140.     pAux->min = min;
  141.     
  142.     /*    set contrlMin field to min or MINSHORT, whichever is greater */
  143.     
  144.     if ( min < MINSHORT )
  145.         min = MINSHORT;
  146.     
  147.     SetControlMinimum( control, min );
  148.         
  149.     /*    reset value */
  150.     
  151.     HsoiLCSetValue( control, pAux->value );
  152.     
  153.     return;
  154. }
  155.  
  156. // set the maximum value of a long control
  157.  
  158. void    HsoiLCSetMax( ControlRef control, long max )
  159. {
  160.     LCAuxPtr        pAux;
  161.     
  162.     pAux = *((LCAuxHandle)GetControlReference( control ));
  163.  
  164.     /*    make sure max is greater than or equal to min */
  165.     
  166.     if ( max < pAux->min )
  167.         max = pAux->min;
  168.     
  169.     /*    save max in auxiliary record */
  170.     
  171.     pAux->max = max;
  172.     
  173.     /*    set contrlMax field to max or MAXSHORT, whichever is less */
  174.     
  175.     if ( max > MAXSHORT )
  176.         max = MAXSHORT;
  177.     
  178.     SetControlMaximum( control, max );
  179.         
  180.     /*    reset value */
  181.     
  182.     HsoiLCSetValue( control, pAux->value );
  183.     
  184.     return;
  185. }
  186.  
  187.  
  188. #pragma mark -
  189. #pragma mark ••• Get Functions •••
  190.  
  191. // in the next 3 functions (HsoiLCGetValue/Min/Max), there is a line of commented out code
  192. // at the end of each function.  All this line of code is is another way of writing the
  193. // function (essentially, 1 line instead of 2).  The commented out line DOES yield tighter
  194. // and smaller code, but it a little harder to read.  Use either (but not both).
  195.  
  196. // get the value of a long control
  197.  
  198. long    HsoiLCGetValue( ControlRef control )
  199. {
  200.     LCAuxPtr    pAux;
  201.     
  202.     pAux = *((LCAuxHandle)GetControlReference( control ));
  203.     return pAux->value;
  204.     
  205. //    return (*(LCAuxHandle)GetControlReference(control))->value;
  206.  
  207. }
  208.  
  209.  
  210. // get the minimum of a long control
  211.  
  212. long    HsoiLCGetMin( ControlRef control )
  213. {
  214.     LCAuxPtr    pAux;
  215.     
  216.     pAux = *((LCAuxHandle)GetControlReference( control ));
  217.     return pAux->min;
  218.     
  219.     // return (*(LCAuxHandle)GetControlReference(control))->min;
  220. }
  221.  
  222.  
  223. // get the maximum of a long control
  224.  
  225. long    HsoiLCGetMax( ControlRef control )
  226. {
  227.     LCAuxPtr    pAux;
  228.     
  229.     pAux = *((LCAuxHandle)GetControlReference( control ));
  230.     return pAux->max;
  231.  
  232.     //    return (*(LCAuxHandle)GetControlReference(control))->max;
  233. }
  234.  
  235.  
  236. #pragma mark -
  237. #pragma mark ••• Synch •••
  238.  
  239. // synchronizing long settings with control (short) settings
  240.  
  241. void    HsoiLCSynch( ControlRef control )
  242. {
  243.     LCAuxPtr        pAux;
  244.     short            controlMin, controlMax, controlValue;
  245.         
  246.     controlMin = GetControlMinimum( control );
  247.     controlMax = GetControlMaximum( control );
  248.     controlValue = GetControlValue( control );    
  249.     pAux = *((LCAuxHandle)GetControlReference( control ));
  250.     
  251.     /*    calculate new long value */
  252.     
  253.     pAux->value = pAux->min + FixMul( FixRatio ( controlValue - controlMin,
  254.                   controlMax - controlMin), pAux->max - pAux->min );
  255.     
  256.     return;
  257. }
  258.